from common.flask_package import *
from model.car_info import *
from model.order import *
from model.vip import *
from model.place import *
from common.my_msg import *
from common.my_fun import *
from datetime import datetime,timedelta
import json

bp_total = Blueprint('total', __name__, url_prefix='/total')
@bp_total.route('/get', methods=['GET'])
def total_get():
    page = request.args.get('page', default=1)
    car_number = request.args.get('car_number')
    if car_number is not None and car_number !='':
        result = Total.total_search(car_number)
        count = 1
    else:
        result, count = Total.query_total_all(page)
    total_list = []
    try:
        for i in result:
            item = {
                'total_id': i.total_id,
                'car_number': i.car_number,
                'time':f'{i.time}',
                'money': i.money,
                'remarks': i.remarks,
            }
            total_list.append(item)

        data = {
            'code': state_ok(),
            'msg': '总金额流水获取成功',
            'count': count,
            'data': total_list
        }
    except Exception as e:
        data = {
            'code': state_error(),
            'msg': '总金额流水获取失败',
            'count': 1,
            'data': total_list
        }

    return Response(json.dumps(data),mimetype='application/json')

# -------------------统计---------------------------
@bp_total.route('/bar/get', methods=['GET'])
def total_bar_get():
    if request.method == 'GET':
        if request.args and request.args.get('time')!='':
            nowtime=request.args.get('time')
        else:
            nowtime=datetime.now().strftime('%Y-%m-%d')
        relust=Total.count_money_time(nowtime)
        data={
            "code":state_ok(),
            'msg':'获取成功',
            'data':relust
        }
    return jsonify(data)